package com.anolesoft.yr.mbus.prod.model.mapper;

import com.anolesoft.yr.mbus.prod.model.entity.ProductionType;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;

import java.util.List;


@Mapper
public interface ProductionTypeMapper {
    @Insert("insert into prod_type(objuid,fac_code,type_code,type_name,type_nature,remark,del_flag," +
            "c_user_uid,c_time,m_user_uid,m_time) values(#{objuid},#{facCode},#{typeCode},#{typeName},#{typeNature},#{remark},0," +
            "#{cUserUid,jdbcType=VARCHAR},#{cTime},#{mUserUid,jdbcType=VARCHAR},#{mTime})")
    int addProduction(ProductionType productionType);

    @Update("update prod_type set del_flag = 1 where objuid=#{id,jdbcType=VARCHAR}")
    int deleteProductionById(@Param("id") String id);

    @Update("update prod_type set type_code=#{typeCode},fac_code=#{facCode},type_name=#{typeName},type_nature=#{typeNature}," +
            "remark=#{remark},m_user_uid=#{mUserUid},m_time=#{mTime,jdbcType=TIMESTAMP} where objuid = #{objuid}")
    int updateProduction(ProductionType productionType);

    @ResultMap("selectProdResult")
    @Select("select objuid,fac_code,type_code,type_name,type_nature,remark,del_flag,c_user_uid,c_time,m_user_uid,m_time from prod_type where del_flag = 0 and objuid=#{objuid}")
    ProductionType queryProductionById(String id);

    @Results(id = "selectProdResult",value = {
            @Result(id = true, column = "objuid", property = "objuid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fac_code", property = "facCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "type_code", property = "typeCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "type_name", property = "typeName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "type_nature", property = "typeNature", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_user_uid", property = "cUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_time", property = "cTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "m_user_uid", property = "mUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "m_time", property = "mTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "del_flag", property = "delFlag", jdbcType = JdbcType.BOOLEAN)
    })
    @Select("select objuid,fac_code,type_code,type_name,type_nature,remark,c_user_uid,c_time,m_user_uid,m_time,del_flag from prod_type where del_flag = 0")
    List<ProductionType> queryProductions();

    @ResultMap("selectProdResult")
    @Select("select objuid,fac_code,type_code,type_name,type_nature,remark,c_user_uid,c_time,m_user_uid,m_time,del_flag from prod_type where del_flag = 0")
    List<ProductionType> selectPageAll(Page<ProductionType> page);

    @ResultMap("selectProdResult")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, type_code, type_name, type_nature, remark, c_user_uid, c_time, m_user_uid, m_time, del_flag from prod_type where del_flag = 0" +
            "<if test =\"o.objuid !=null and o.objuid != ''\"> and objuid = #{o.objuid, jdbcType=VARCHAR} </if>" +
            "<if test =\"o.typeCode !=null and o.typeCode != ''\"> and type_code = #{o.typeCode, jdbcType=VARCHAR} </if>" +
            "<if test =\"o.typeName !=null and o.typeName != ''\"> and type_name = #{o.typeName, jdbcType=VARCHAR} </if>" +
            "<if test =\"o.typeNature !=null and o.typeNature != ''\"> and type_nature = #{o.typeNature, jdbcType=VARCHAR} </if>" +
            "</script>")
    List<ProductionType> queryList(@Param("o") ProductionType productionType);

   @ResultMap("selectProdResult")
   @Select("<script>" +
            "select " +
            "objuid, fac_code, type_code, type_name, type_nature, remark, c_user_uid, c_time, m_user_uid, m_time, del_flag from prod_type where del_flag = 0" +
            "<if test =\"o.typeCode !=null and o.typeCode != ''\"> and type_code = #{o.typeCode, jdbcType=VARCHAR} </if>" +
            "<if test =\"o.typeName !=null and o.typeName != ''\"> and type_name = #{o.typeName, jdbcType=VARCHAR} </if>" +
            "<if test =\"o.typeNature !=null and o.typeNature != ''\"> and type_nature = #{o.typeNature, jdbcType=VARCHAR} </if>" +
            "</script>")
    List<ProductionType> queryListPage(@Param("o") ProductionType productionType, Page<ProductionType> page);

   @ResultMap("selectProdResult")
   @Select("<script>" +
            "select " +
            "objuid, fac_code, type_code, type_name, type_nature, remark, c_user_uid, c_time, m_user_uid, m_time, del_flag from prod_type where del_flag = 0" +
            "<if test=\"o.typeCode!=null or o.typeName!=null\">" +
                "and (" +
                "<trim prefixOverrides=\"or\">" +
                "<if test =\"o.typeCode !=null and o.typeCode != '' \">" +
                "  or type_code like concat(#{o.typeCode, jdbcType=VARCHAR},'%') " +
                "</if>" +
                "<if test =\"o.typeName !=null and o.typeName != '' \">" +
                "   or type_name like concat(#{o.typeName, jdbcType=VARCHAR},'%') " +
                "</if>" +
                "</trim>" +
                ")" +
            "</if>" +
            "</script>")
    List<ProductionType> queryComboxList(@Param("o") ProductionType productionType, Page<ProductionType> page);
}
